#include <fstream>
#include "make_graph_file.h"
using namespace std;

// Make a file with the graph and print ot the (weights,flow,y_var)
// at the edges. In the node the node price is given.
void create_graph_file(const char* filename, network *net, 
		      vector<double> &flow, vector<double> &w, 
		      vector<int> &y_var, vector<int> &node_price)
{
  int nr_arcs = net->linktablerows();
  int nr_nodes = net->nodes();
  vector<int> arc(nr_arcs);
  ofstream myfile;
  pair<double, double> coord;
  myfile.open(filename);
  myfile << "graph G \{\n edge [len=3];\n";
  // Print the graph plus the edge label
  for(int i=0; i < nr_arcs; ++i){
    arc = net->linktablerow(i);
    coord = net->get_node_coordinates(i);
    myfile << "N" << arc[0]+1 << " -- N" << arc[1]+1 
	   << " [label=\"(" << w[i] << "," << flow[i] 
	   << "," << y_var[i] << ")\"];\n";
    myfile << "N" << i+1 << " [ pos = \"" << coord.first 
	   << "," << coord.second << "!\" ];\n"; 
  }
  // Fix the node label
  for(int i=0; i < nr_nodes; ++i){
    coord = net->get_node_coordinates(i);
    myfile << "N" << i+1 << "[label=\"N" << i+1
	   << "\\n pi: " << node_price[i] << "\"];\n";
    //myfile << "N" << i+1 << " [ pos = \"" << coord.first 
    //	   << "," << coord.second << "!\" ];\n"; 
  }
  myfile << "}\n";
  myfile.close();
}



// Make a file with the graph and print ot the flow at the edge
void create_graph_file(const char* filename, network *net, vector<double> &flow)
{
  int nr_arcs = net->linktablerows();
  int nr_nodes = net->nodes();
  vector<int> arc(nr_arcs);
  pair<double, double> coord;
  
  ofstream myfile;
  myfile.open(filename);
  myfile << "digraph G \{\n edge [len=3];\n";
  for(int i=0; i < nr_nodes; ++i){
    coord = net->get_node_coordinates(i);
    myfile << "N" << i+1 << " [ pos = \"" << coord.first 
    	   << "," << coord.second << "!\" ];\n"; 
  }
  for(int i=0; i < nr_arcs; ++i){
    arc = net->linktablerow(i);
    myfile << "N" << arc[0]+1 << " -> N" << arc[1]+1 
	   << " [label=\"" << flow[i] << "\"];\n";
  } 
  myfile << "}\n";
  myfile.close();
}

// Make a file with the graph and print ot the flow at the edge
void create_graph_file(const char* filename, network *net)
{
  int nr_arcs = net->linktablerows();
  int nr_nodes = net->nodes();
  vector<int> arc(nr_arcs);
  vector<double> cap(nr_arcs);
  pair<double, double> coord;
  
  ofstream myfile;
  myfile.open(filename);
  myfile << "digraph G \{\n edge [len=3];\n";;
  for(int i=0; i < nr_nodes; ++i){
    coord = net->get_node_coordinates(i);
    myfile << "N" << i+1 << " [ pos = \"" << coord.first 
    	   << "," << coord.second << "\" ];\n"; 
  }
  for(int i=0; i < nr_arcs; ++i){
    arc = net->linktablerow(i);
    myfile << "N" << arc[0]+1 << " -> N" << arc[1]+1 
	   << " [label=\"" << i << "\"];\n";
  } 
  myfile << "}\n";
  myfile.close();
}

void create_graph_file_SP(const char* filename, network *net, vector<int> &SP)
{
  int nr_arcs = net->linktablerows();
  int nr_nodes = net->nodes();
  int demands = SP.size()/nr_arcs;
  vector<int> arc(nr_arcs);
  pair<double, double> coord;
  
  ofstream myfile;
  myfile.open(filename);
  myfile << "digraph G \{\n edge [len=1];\n";
  for(int j=0; j < demands; ++j){
    //myfile << "\t subgraph cluster" << j << " \{\n";
    for(int i=0; i < nr_nodes; ++i){
      coord = net->get_node_coordinates(i);
      myfile << "\t N" << i+1 << "_" << j+1 << " [ pos = \"" << coord.first + 500*(j/7) 
	     << "," << coord.second + 500*(j/7) << "\"];\n";
    }
    
    for(int i=0; i < nr_arcs; ++i){
      if(SP[i+j*nr_arcs] != 0){
	arc = net->linktablerow(i);
	myfile << "\t N" << arc[0]+1 << "_" << j+1 << " -> N" << arc[1]+1 << "_" << j+1
	       <<  ";\n";
      }
    } 
    //myfile << "\t }\n";
  }
  myfile << "}\n";
  myfile.close();
}

//Test program
/*int main ()
{
  network *net = new(network);
  net->read_network_file("atlanta.txt");
  vector<double> flow(22);
  vector<int> y_var(22);
  vector<double> w(22);
  vector<int> node_price(15);
  for(int i = 0; i < 22; ++i){
  	flow[i] = rand()%20;
	w[i] = rand()%100;
	y_var[i] = rand()%2;
  }
  
  for(int i = 0; i < 15; ++i){
  	node_price[i] = rand()%20;
  }
  creat_graph_file("Graph1.dot",net,flow,w,y_var,node_price);
  flow.clear();
  return 0;
  }*/

